package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//@CrossOrigin //跨域 前后端端口
@RestController
@Tag(name = "登录登出模块")
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    ValidateCodeService validateCodeService;




    @Operation(summary = "退出功能")
    @GetMapping("logout")
    //退出实现根据token去删除redis的数据
    public Result logout(@RequestHeader String token){
        sysUserService.logout(token);
        //退出接头 不返回东西
        return   Result.build(null,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "根据请求头token查询用户信息")
    @GetMapping("getUserInfo")
    public Result getUserInfo(@RequestHeader String token){

        SysUserVo SysUserVo = sysUserService.getUserInfo(token); //id + name + avatar
        return Result.build(SysUserVo,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "登录接口") //对接口方法的一个描述
    //传的LoginVo里面有username password的字段这正是我们需要的
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto){ //ctrl+alt+b 直接进去实现类的方法
        //后端给前端返回token loginVo里面有token
        LoginVo loginVo = sysUserService.login(loginDto); //loginVo里面传的对象数据是data
        //传回去的参数 data code message
        //data里面是token refreshtoken
        //return Result.build(loginVo,200,"登陆成功")
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取图形验证码")
    @GetMapping("genreteValidateCode")
    public Result genreteValidateCode(){
        //codekey codevalue
        //codevalue 图形验证码 codekey uuid字符串暂存验证码
       ValidateCodeVo validateCodeVo =  validateCodeService.generaterValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);

    }
}
